//
//  MyLayout.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/7/10.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "MyLayout.h"

@implementation MyLayout
//3个方法
//return collectonView contentsize
-(CGSize)collectionViewContentSize{
    //    NSLog(@"collectionViewContentSize%@",NSStringFromCGSize([UIScreen mainScreen].bounds.size));
    //    比较左边 和右边的高度哪个大
    CGFloat left=0.0;
    CGFloat right=0.0;
    for (int i=0; i<_dataArray.count; i++) {
        CGFloat h=[_dataArray[i] imgHight];
        if (left<=right) {
            left+=h;
        }else{
            right+=h;
        }   
    }
    return CGSizeMake(0, left>right?left:right);
}

-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes *arr=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat cellHeight=[_dataArray[indexPath.row]imgHight];
    CGFloat left=0.0;
    CGFloat right=0.0;
    //    循环到当前cell，计算出当前indexpath的cell 高度，中心
    for (int i=0; i < indexPath.row; i++) {
        CGFloat h=[_dataArray[i]imgHight];
        if (left<=right) {
            left+=h;
        }else{
            right+=h;
        }
    }
    if (left<=right) {
        arr.center=CGPointMake(kScreenWidth/2/2, left+cellHeight/2);
    }else{
        arr.center=CGPointMake(kScreenWidth/2 + kScreenWidth/4, right+cellHeight/2);
    }
    arr.size=CGSizeMake((kScreenWidth-30)/2, cellHeight);
    return arr;
    
}

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    
    //    NSLog(@"layoutAttributesForElementsInRect =%d",[self.collectionView numberOfItemsInSection:0]);
    for (int i=0; i<[self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *indexpath=[NSIndexPath indexPathForItem:i inSection:0];
        [arr addObject:[self layoutAttributesForItemAtIndexPath:indexpath]];
    }
    return arr;
    
}
@end
